<template>
<div style="padding: 10px">
  <a-row type="flex">
    <a-col flex="200px" >
      <div style="text-align: center;">
        <a-menu style="width: 150px;" mode="vertical" @click="handleClick" :default-selected-keys="['3']">
            <a-menu-item key="1" v-if="userType === '1' || userType === '0' ">
              用户列表
            </a-menu-item>
            <a-menu-item key="2" v-if="userType === '1' || userType === '0' ">
              新增用户
            </a-menu-item>
            <a-menu-item key="3">
              转账交易
            </a-menu-item>
            <a-menu-item key="4" >
              注销退出
            </a-menu-item>
          </a-menu>
      </div>
    </a-col>
    <a-col flex="auto">
      <user-infos v-if="current === 1"></user-infos>
      <user-info v-if="current === 2"></user-info>
      <transfer v-if="current === 3"></transfer>
    </a-col>
  </a-row>
  <!-- <a-tabs default-active-key="1" tab-position="left" @change=changeTab>
    <a-tab-pane key="1" v-if="userType === '1' || userType === '0' ">
      <span slot="tab">
          用户列表
        </span>
      <user-infos ></user-infos>
    </a-tab-pane>
    <a-tab-pane key="2" v-if="userType === '1' || userType === '0' ">
      <span slot="tab">
         新增用户
        </span>
      <user-info></user-info>
    </a-tab-pane>
    <a-tab-pane key="3">
      <span slot="tab">
         转账交易
        </span>
      <transfer></transfer>
    </a-tab-pane>
    <a-tab-pane key="4">
      <span slot="tab">
         注销退出
        </span>

    </a-tab-pane>
  </a-tabs> -->


</div>
</template>

<script>
import Constant from "../utils/Constant";
import UserInfos from "../components/UserInfos.vue";
import UserInfo from "../components/UserInfo.vue";
import Transfer from '../components/Transfer.vue';

export default {
  components: {
    UserInfos,
    UserInfo,
    Transfer
  },
  data() {
    return {
      authorization: "",
      userType: "1",
      current: 3,
      logined: false,
    };
  },
  created() {
    console.log("created dashborad");
    this.authorization = localStorage.getItem("authorization");
    this.userType = localStorage.getItem("userType");

    //判断是否登陆
    if (!this.authorization) {
      this.gotoLogin();
    }
    this.checkLogin();

    // this.ebus.$on("logined", (msg) => {
    //   console.log(msg);
    // })
    this.$eventbus.$emit("selectMenu", { name: "Accounts"});
  },
  methods: {
    handleClick(e) {
      // console.log("click ", e);
      if(e.key === "1") {
          this.current = 1;
      } else if(e.key === "2") {
        this.current =2;
      } else if(e.key === "3") {
        this.current =3;
      } else if(e.key === "4") {
          this.$confirm({
          title: '消息提示',
          content: '确认退出?',
          onOk: () => {
            // return new Promise((resolve, reject) => {
            //   setTimeout(Math.random() > 0.5 ? resolve : reject, 1000);
            // }).catch(() => console.log('Oops errors!'));\
            this.logout();
          },
          onCancel() {},
        });
      }
    },
    checkLogin() {
      let url = Constant.LOGIN_CHECK_URL;
      let params = {};
      params.authorization = this.authorization;
      this.$http.post(url, params).then(
        (resp) => {
          console.log(resp);
          if (resp.data.code === "01") {
            if (resp.data.result) {
              this.gotoLogin();
              this.logined = true;
            } else {
              // localStorage.removeItem("authorization");
              this.logined = false;
            }
          }
        },
        (err) => {
          console.log(err);
        }
      );
    },
    gotoLogin() {
      this.$router.push({
        name: "Login"
      });
    },
    changeTab(e) {
      // console.log(e);
      if (e === "4") {
        console.log(e, 4);
        this.$confirm({
          title: '消息提示',
          content: '确认退出?',
          onOk: () => {
            // return new Promise((resolve, reject) => {
            //   setTimeout(Math.random() > 0.5 ? resolve : reject, 1000);
            // }).catch(() => console.log('Oops errors!'));\
            this.logout();
          },
          onCancel() {},
        });
      //} else if(e === "1") {

      }
    },
    logout() {
      this.logined = false;
      localStorage.removeItem("authorization");
      localStorage.removeItem("userType");
      this.$router.push({
        name: "Blocks"
      })
    }
  },
};
</script>
